package log

import (
	"os"
	"time"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xiaoyutab/xgotool/individual/xcmd"
)

// 日志压缩事件
//
//	log.p7z	<nil>
func P7z(param []byte) error {
	// 因日志固定为sqlite，所以此处直接使用sqlite进行连接，不再进行数据库判断
	pth := config.GetExt().RunConf.LogPath
	if pth == "" {
		return nil
	}
	// 检测昨天的日志是否存在，若存在则进行压缩并删除
	for i := -7; i < 0; i++ {
		if _, err := os.Stat(pth + "/" + time.Now().AddDate(0, 0, i).Format(time.DateOnly) + ".db"); err == nil {
			if config.GetExt().RunConf.P7zPath != "" {
				xcmd.Exec(config.GetExt().RunConf.P7zPath, "a", pth+"/"+time.Now().AddDate(0, 0, i).Format(time.DateOnly)+".7z", pth+"/"+time.Now().AddDate(0, 0, i).Format(time.DateOnly)+".db")
				os.Remove(pth + "/" + time.Now().AddDate(0, 0, i).Format(time.DateOnly) + ".db")
			}
		}
	}
	return nil
}
